$(function () {
    $("#jqGrid").Grid({
        url: '../devmaintenanceplanchange/list',
        colModel: [
            {label: '工单编号', name: 'ticketCode', index: 'REPAIR_CODE', width: 80},
            {label: '原因', name: 'reason', index: 'REASON', width: 80},
            {label: '事项', name: 'item', index: 'ITEM', width: 80},
            {label: '当前结束日期', name: 'originalTime', index: 'END_TIME', width: 80,formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd');
                }
            },
            {label: '目标结束日期', name: 'changeTime', index: 'CHANGE_TIME', width: 80,formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd');
                }
            },
            {label: '进度', name: 'progress', index: 'PROGRESS', width: 80,formatter: function (value) {
                    return sysEnums.SPARE_NEEDS_PLAN_PROGRESS[value];
                }
            },
            {label: '申请人', name: 'applierName', index: 'APPLIER', width: 80},
            {label: '申请时间', name: 'gmtCreate', index: 'GMT_CREATE', width: 80},
            {label: '审批人', name: 'checkerName', index: 'CHECKER', width: 80},
            {label: '审批时间', name: 'gmtModified', index: 'GMT_MODIFIED', width: 80},
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true}
        ]
    });
    vm.initDeviceOrgList();
});

var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url: "nourl"
        }
    },
    callback: {
        beforeCheck: true
    }

};

//设备组织树
var deptSetting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url: "nourl",
            name: "complexName"
        }
    },
    callback: {
        beforeCheck: true,
        beforeClick: function (e, treeObj, treeNode) {
            vm.q.devLineId = treeObj.deptId;
            vm.query();
        }
    }
};

function outTreeCheck(e, treeObj, treeNode) {
    vm.q.deptCode = treeObj.deptId;
    vm.query();
}

let vm = new Vue({
    el: '#rrapp',
    data: {
        dataTime:[],//时间范围框绑定
        showList: true,
        title: null,
        devMaintenancePlanChange: {
            progress: '',
            applier: '',
            changeTime: '',
            reason: '',
            item: '',
            ticketId: '',
            type: '',
            id: '',
        },
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            ticketCode: ''
        }
    },
    methods: {
        initDeviceOrgList: function () {
            //加载部门树
            Ajax.request({
                url: '../sys/user/treeDeptBySCXLevel?level=3',
                async: false,
                successCallback: function (r) {
                    deptZtree = $.fn.zTree.init($("#storeTree"), deptSetting, r.list);
                    // 默认展开工厂一级
                    deptZtree.expandNode(deptZtree.getNodeByParam("deptId",'01',null));
                }
            });
        },


        dataChange:function (e) {
            if(e.length>0){
                vm.q.beginTime = e[0];
                vm.q.endTime = e[1]
            }else{
                vm.q.beginTime = '';
                vm.q.endTime ='';
            }
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.devMaintenancePlanChange = {
                progress: '',
                applier: '',
                changeTime: '',
                reason: '',
                item: '',
                ticketId: '',
                type: '',
                id: '',
            };
        },
        update: function (event) {
            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            let url = vm.devMaintenancePlanChange.id == (null || '') ? "../devmaintenanceplanchange/save" : "../devmaintenanceplanchange/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.devMaintenancePlanChange),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../devmaintenanceplanchange/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../devmaintenanceplanchange/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.devMaintenancePlanChange = r.devMaintenancePlanChange;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: vm.q,
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                ticketCode: '',
                beginTime:'', // 开始时间
                endTime:'' ,// 结束时间,
                devLineId: '',
            };
            // 取消选中
            deptZtree.cancelSelectedNode();
            vm.dataTime = [];
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
    }
});
